import axios from 'axios';
import { Message} from 'element-ui';
import store from '../store/store';
import { getConfig } from '../api/apis';
const service = axios.create({
    baseURL: '',
    timeout: 90000,
    withCredentials: true
});

getConfig().then(response=>{
    service.defaults.baseURL = response.API_SERVER_URL;
})

service.interceptors.request.use(
    config => {
        config.headers.process_type = store.state.app.processType;
        // const user = store.state.app.cloudUserInfo;
        // if (user && user.jwtRaw && typeof user.jwtRaw === 'string' && user.jwtRaw.length > 0) {
        //     config.headers.Authorization = 'Bearer ' + user.jwtRaw;
        // }
        return config;
    },
    error => {
        console.log(error);
        return Promise.reject(error);
    }
);

service.interceptors.response.use(
    response => {
        if (response.status === 200 && response.data.code == 401) {
          store.dispatch('app/setAppUserInfo', null);
          location.href = '/#/login';
          return Promise.resolve({code:0,msg:'登陆失效,请重新登陆',data:null});
        }
        return response.data;
    },
    error => {
        if(error && error.response && error.response.status === 401){
          store.dispatch('app/setAppUserInfo', null);
          location.href = '/#/login';
          return Promise.resolve({code:0,msg:'登陆失效,请重新登陆',data:null});
        }
        return Promise.resolve({code:-100,msg:'网络错误',data:null});
    }
);

/**
 * post 方法封装
 * @param url
 * @param data
 * @returns {Promise}
 */
function post(url, data = {}, config = {}, isShowError = true) {
    return new Promise((resolve, reject) => {
        service.post(url, data, config).then(
            response => requestResolveHandler(response, resolve,reject,isShowError),
            err => requestRejectHandlerBuilder(err, reject)
        );
    });
}
/**
 * get 方法封装
 * @param url
 * @param data
 * @returns {Promise}
 */
function get(url, data = {}, isShowError = true) {
    return new Promise((resolve, reject) => {
        service.get(url, { params: data }).then(
            response => requestResolveHandler(response, resolve,reject,isShowError),
            err => requestRejectHandlerBuilder(err, reject)
        );
    });
}


/**
 * delete 方法封装
 * @param url
 * @param data
 * @returns {Promise}
 */
function del(url, data = {}, isShowError = true) {
    return new Promise((resolve, reject) => {
        service.delete(url, data).then(
            response => requestResolveHandler(response, resolve,reject,isShowError),
            err => requestRejectHandlerBuilder(err, reject)
        );
    });
}
/**
 * get 方法封装
 * @param url
 * @param data
 * @returns {Promise}
 */
function put(url, data = {}, config = {}, isShowError = true) {
    return new Promise((resolve, reject) => {
        service.put(url, data, config).then(
            response => requestResolveHandler(response, resolve,reject,isShowError),
            err => requestRejectHandlerBuilder(err, reject)
        );
    });
}


function requestResolveHandler(response, resolve, reject,isShowError) {
    if (response.code === 1) {
        return resolve(response);
    } else {
        if (isShowError) {
            const errorMsg = response && response.msg ? response.msg : '出错了';
            Message.warning(errorMsg);
        }
        return reject(response);
    }
}


function requestRejectHandlerBuilder(err, reject) {
    console.log('err',err);
    Message.error('网络错误');
    return reject(err);
}
export default { post, get, put, del };
